package OpUtils;
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

my @opList;
my $DEBUG;

#key used in operator.xml
my $keyOp = 'operator';
my $keyRegion = 'region';
my $keyPlmn = 'mccmnc';
my $keyOpName = 'opName';

BEGIN {
	#print "MyUtils load\n";
	my $opXml = "utils/operator.xml";
	if (-e $opXml) {
		my $opRef = XMLin($opXml, ForceArray=>1);
		@opList = @{$opRef->{'operator'}};
		print Dumper(\@opList) if ($DEBUG);
	} else {
		print "MyUtils : can't find $opXml" if ($DEBUG);
	}
}

sub getOpsByRegion {
	my $region = shift;
	my @retList;
	foreach (@opList) {
		push @retList, $_->{$keyOpName} if $_->{$keyRegion} eq $region;
	}
	my %count;
	@retList = grep {++$count{$_} < 2;} @retList;
	return @retList;
}
sub getPlmnsByOp {
    my $op = shift;
	my @retList;
	foreach (@opList) {
	    return @{$_->{$keyPlmn}} if ($_->{$keyOpName} eq $op);
	}
}
sub getRegionByPlmn {
	my $plmn = shift;
	foreach (@opList) {
		return $_->{$keyRegion} if (grep /$plmn/, @{$_->{$keyPlmn}});
	}
}
sub getOpByPlmn {
	my $plmn = shift;
	foreach (@opList) {
		return $_->{$keyOpName} if (grep /$plmn/, @{$_->{$keyPlmn}});
	}
}


END {
	#print "MyUtils :: end\n";
}
1;